<?php

class Soul_Annotation_Parser {
    
    private static $_attributes = array();    

    public static function registerAttribute($name, $functionName) {
        self::$_attributes[$name] = new Soul_Annotation_Attribute($name, $functionName);
    }

    public static function removeAttribute($name) {
        unset(self::$_attributes[$name]);
    }

    public static function attributeIsRegistry($name) {
        return isset(self::$_attributes[$name]);
    }   
    
    public static function parseFromClass(ReflectionClass $class){
        return self::parse($class->getDocComment());
    }
    
    public static function parseFromFunction(ReflectionFunction $function){
        return self::parse($function->getDocComment());
    }
    
    public static function parseFromMethod(ReflectionMethod $method){
        return self::parse($method->getDocComment());
    }
    
    public static function parse($string){
        $result = array();
        if(preg_match_all('/#(\w+)\s+(\S*)/i', $string, $matches)){
            $len = count($matches[1]);
            for($i = 0; $i < $len; $i++){
                if(self::attributeIsRegistry($matches[1][$i])){
                    $args = array();
                    if(!empty($matches[2][$i])){
                        parse_str($matches[2][$i], $args);
                    }
                    $result[] = array(
                      'attribute'=>self::$_attributes[$matches[1][$i]],
                      'arguments'=>$args
                    );
                }
            }
        }
        return $result;
    }
}
